package com.yami.shop.api.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.yami.shop.bean.dto.HotSearchDto;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.HotSearchService;
import com.yami.shop.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author LGH
 */
@RestController
@RequestMapping("/search")
@Tag(name = "搜索接口")
public class SearchController {

    @Autowired
    private HotSearchService hotSearchService;

    @Autowired
    private ProductService productService;

    @GetMapping("/hotSearchByShopId")
    @Operation(summary =  "查看店铺热搜", description = "根据店铺id,热搜数量获取热搜")
    @Parameters({
            @Parameter(name = "shopId", description = "店铺id", required = true),
            @Parameter(name = "number", description = "取数", required = true),
            @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)"),
    })
    public ServerResponseEntity<List<HotSearchDto>> hotSearchByShopId(Long shopId, Integer number, Integer sort) {
        List<HotSearchDto> list = hotSearchService.getHotSearchDtoByshopId(shopId);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/hotSearch")
    @Operation(summary =  "查看全局热搜", description = "根据店铺id,热搜数量获取热搜")
    @Parameters({
            @Parameter(name = "number", description = "取数", required = true),
            @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)"),
    })
    public ServerResponseEntity<List<HotSearchDto>> hotSearch(Integer number, Integer sort) {
        List<HotSearchDto> list = hotSearchService.getHotSearchDtoByshopId(0L);
        return ServerResponseEntity.success(list);
    }

    private List<HotSearchDto> getListResponseEntity(Integer number, Integer sort, List<HotSearchDto> list) {
        if (sort == null || sort == 0) {
            Collections.shuffle(list);
        }
/*        if(sort==1){
            Collections.sort(list, new SeqComparator()); // 根据排序号排序
        }*/
        if (!CollectionUtil.isNotEmpty(list) || list.size() < number) {
            return list;
        }
        return list.subList(0, number);
    }
}
